// // Copyright (c) 2009 All Right Reserved // // vl // // 2009-01-01 // Contains ... // Structure to represent one instrument on channel. namespace LargoCommon.Music { using Abstract; using JetBrains.Annotations; using LargoCommon.Interfaces; using Localization; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Globalization; using System.Linq; using System.Xml.Linq; /// /// General Instrument. /// public sealed class MusicalInstrument { #region Constructors /// /// Initializes a new instance of the class. /// /// The given instrument. public MusicalInstrument(MidiMelodicInstrument givenInstrument) { this.Genus = InstrumentGenus.Melodical; this.Number = (byte)givenInstrument; } /// /// Initializes a new instance of the class. /// /// The given instrument. public MusicalInstrument(MidiRhythmicInstrument givenInstrument) { this.Genus = InstrumentGenus.Rhythmical; this.Number = (byte)givenInstrument; } /// /// Initializes a new instance of the class. /// /// The given instrument number. /// Type of the given line. public MusicalInstrument(byte givenInstrumentNumber, MusicalLineType givenLineType) { this.Number = givenInstrumentNumber; if (givenLineType == MusicalLineType.Melodic) { this.Genus = InstrumentGenus.Melodical; } if (givenLineType == MusicalLineType.Rhythmic) { this.Genus = InstrumentGenus.Rhythmical; } } /// /// Initializes a new instance of the class. /// /// The tones. public MusicalInstrument(IEnumerable tones) { Contract.Requires(tones != null); this.TakeInstrumentFrom(tones); } /// /// Initializes a new instance of the class. /// /// The tones. public MusicalInstrument(IEnumerable tones) { Contract.Requires(tones != null); this.TakeInstrumentFrom(tones); } /// /// Initializes a new instance of the class. /// /// The mark instrument. public MusicalInstrument(XElement markInstrument) { this.Genus = DataEnums.ReadAttributeInstrumentGenus(markInstrument.Attribute("Genus")); this.Number = XmlSupport.ReadByteAttribute(markInstrument.Attribute("Number")); } /// /// Initializes a new instance of the class. /// public MusicalInstrument() { } #endregion #region Properties - Xml /// /// Gets the get x element. /// /// /// The get x element. /// public XElement GetXElement { get { XElement mainElement = null; if (this.Genus == InstrumentGenus.Melodical) { mainElement = new XElement( "Instrument", new XAttribute("Genus", this.Genus), new XAttribute("Name", this.MelodicInstrument), new XAttribute("Number", (byte)this.MelodicInstrument)); } else if (this.Genus == InstrumentGenus.Rhythmical) { mainElement = new XElement( "Instrument", new XAttribute("Genus", this.Genus), new XAttribute("Name", this.RhythmicInstrument), new XAttribute("Number", (byte)this.RhythmicInstrument)); } return mainElement; } } #endregion #region Properties /// /// Gets or sets the instrument genus. /// /// /// The genus. /// public InstrumentGenus Genus { get; set; } /// /// Gets the melodic instrument. /// /// /// The melodic instrument. /// public MidiMelodicInstrument MelodicInstrument { get { if (this.Genus == InstrumentGenus.Melodical) { return (MidiMelodicInstrument)this.Number; } else { return MidiMelodicInstrument.None; } } } /// /// Gets the rhythmic instrument. /// /// /// The rhythmic instrument. /// public MidiRhythmicInstrument RhythmicInstrument { get { if (this.Genus == InstrumentGenus.Rhythmical) { return (MidiRhythmicInstrument)this.Number; } else { return MidiRhythmicInstrument.None; } } } /// /// Gets the melodic section. /// /// /// The melodic section. /// [UsedImplicitly] public MidiMelodicSection MelodicSection { get { if (this.Genus == InstrumentGenus.Melodical) { return (MidiMelodicSection)this.Section; } else { return MidiMelodicSection.None; } } } /// Gets or sets the file name. /// Property description. /// Returns value. public byte Number { get; set; } /// Gets or sets the file name. /// Property description. /// Returns value. public byte Section { get; set; } /// /// Gets a value indicating whether this instance is empty. /// /// /// true if this instance is empty; otherwise, false. /// public bool IsEmpty => this.Number == 127; #endregion #region String representation /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// /// General musical property. public override string ToString() { //// Assumed melodic track (not rhythmic instrument...) if (this.Genus == InstrumentGenus.Melodical) { return LocalizedMusic.String("MelInstr" + this.Number.ToString(CultureInfo.CurrentCulture)); } else { return LocalizedMusic.String("RhyInstr" + this.Number.ToString(CultureInfo.CurrentCulture)); } } #endregion #region Private methods /// /// Takes the instrument and channel from. /// /// The tones. private void TakeInstrumentFrom(IEnumerable tones) { Contract.Requires(tones != null); this.Genus = InstrumentGenus.None; var melTone = (from t in tones where !t.IsPause select t).FirstOrDefault(); if (melTone == null) { return; } this.Genus = InstrumentGenus.Rhythmical; this.Number = melTone.InstrumentNumber; } /// /// Takes the instrument and channel from. /// /// The tones. private void TakeInstrumentFrom(IEnumerable tones) { Contract.Requires(tones != null); this.Genus = InstrumentGenus.None; var melTone = (from t in tones where !t.IsPause select t).FirstOrDefault(); if (melTone == null) { return; } this.Genus = InstrumentGenus.Melodical; this.Number = melTone.InstrumentNumber; } #endregion } }